package logic

import (
	"context"
	"errors"
	"math/rand"
	"strconv"
	"supplier/models/cache"
	"supplier/pkg"
	"time"

	"supplier/supplierrpc/internal/svc"
	"supplier/supplierrpc/supplierrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *supplierrpc.SendSmsRequest) (*supplierrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line

	code := rand.Intn(9000) + 1000

	sum, err := pkg.SendSum(in.Account, strconv.Itoa(code))
	if err != nil {
		return nil, err
	}
	if sum.Code != 2 {
		return nil, errors.New(sum.Msg)
	}
	cache.Rdb.Set(l.ctx, "sendSms"+in.Account, code, time.Minute*5)
	return &supplierrpc.SendSmsResponse{}, nil
}
